Welcome Guest | Sign in | Register

Home > Java Programming > Flow Control > Questions and Answers

01. which has public static void main(), so it will throw runtime error. what will be the out put of following code ?

G.java

class F{
static void main(String args[]){
System.out.println(“G”);
}
}

class G{
static void main(String args[]){
System.out.println(“F”);
}
}
A. compile time error B. run time error

C. F D. G

Answer and Explanation

Answer: F

Explanation:
here, in a single file we have declared two class which has main method. The name using which you will save that class will execute and display the output.you can write multiple main methods in java only in different class ,not in a single class.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. class F{
Private static void main(String args[]){
System.out.println(“HI”);
}
}
A. compile time error B. run time error
C. HI D. no output

Answer and Explanation

Answer: run time error

Explanation:
here private keyword is used in main() method signature, compiler will check whether main() method is declared properly or not , According to the compiler its correct, because private access specifier declaration is correct , its upto the developer to decide when to use public, private and all . BUT during runtime JVM will check the signature of main() method , but its wrong, because JVM will be searching for the method which has public static void main(), so it will throw runtime error. 

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. class A{
Public static void main(String args[]){
System.out.println(“ main method starts ”);
test();
test();
test();
System.out.println(“ main method ends”);
}
Public static void test(){
System.out.println(“test”);
}
}
A. main method starts
test
test
test
main method ends
B. compile time error
C. run time error D. main method starts
main method ends
test
test
test

Answer and Explanation

Answer: main method starts
test
test
test
main method ends

Explanation:
here the code which we have in main method will execute, and test() method we are calling three times from main so test() method will execute three times. To call test() method no need to create an object because test() method is declared as static ,so memory will be allocated automatically. You can call directly.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04.  class A{
Public static void main(String args[]){
System.out.println(“ main method starts ”);
int i=10;
System.out.println(i);
}
Public static void test(){
System.out.println(i);
}
}
A. compile time error B. run time error
C. 10 D. no output

Answer and Explanation

Answer: compile time error

Explanation:
here variable I is local variable , it is accessible only within the main() method , but in the above code they are trying to access from test() method ,so it will throw an compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. class A{
Public static void main(String args[]){
System.out.println(“ main method starts ”);
int i=10;
test();test(i);test(10);
}
Public static void test(int i){
System.out.println(i);
}
}
A. compile time error B. run time error
C. 10 D. no output

Answer and Explanation

Answer: compile time error

Explanation:
in the above code from main() method we are calling test() method without parameter but we don’t have test() method signature like without parameter, so compiler is unable to recognize test() method so it will throw an error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. class A{
Public static void main(String args[]){
int i=10;
test(i);test(10); test(20);
}
Public static void test(int i){
System.out.println(i);
}
}
A. compile time error B. run time error
C. 10 10 20 D. no output

Answer and Explanation

Answer: 10 10 20

Explanation:
here the code which we have in main method will execute, and test() method we are calling three times from main by passing I value and two constant values like 10,20 so test() method will execute three times. To call test() method no need to create an object because test() method is declared as static ,so memory will be allocated automatically. You can call directly.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. class A{
Public static void main(String args[]){
int i=10;
i=test(i);
System.out.println(i);
}
Public static int test(int i){
i=200;
return I;

}
}
A. compile time error B. run time error
C. 10 D. 200

Answer and Explanation

Answer: 200

Explanation:
here the code which we have in main method will execute, and test() method we are calling by passing I variable value , and that test() method returns value 200, that value will be stored in I variable , so previous value of the I variable is vanished now , new value 200 will be stored in I variable , then that value will be printed in output. To call test() method no need to create an object because test() method is declared as static ,so memory will be allocated automatically. You can call directly.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. class A{
Public static void main(String args[]){
int i;
System.out.println(i);
}
}
A. compile time error B. run time error
C. no out put D. 0

Answer and Explanation

Answer: compile time error

Explanation:
here I variable is a local variable , local variable should be initialized before the use , but they are using without initializing that’s why it is throwing compile time error.


Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. class A{
Public static void main(String args[]){
int I,j;
i=j;
System.out.println(i);
}
}
A. compile time error B. run time error
C. no output D. 0

Answer and Explanation

Answer: compile time error

Explanation:
here I and j variable is a local variable , local variable should be initialized before the use , but they are trying to assign value of j to I without initializing ,that’s why it is throwing compile time error.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. class A{
Public static void main(String args[]){
int I=10;
test(i)
System.out.println(i);
}
Public static int test(int i){

System.out.println(i);
}
}
A. compile time error B. run time error
C. 10 10 D. 10

Answer and Explanation

Answer: 10 10

Explanation:
here the code which we have in main method will execute, and test() method we are calling from main by passing I value so test() method will execute and display the I value . To call test() method no need to create an object because test() method is declared as static ,so memory will be allocated automatically. You can call directly.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved 2012-2015 SoftLucent.